<?php
declare(strict_types=1);

namespace Dreamcat\ApiDocTest\TestCases;

use Dreamcat\ApiDoc\ApiGenerators\ApiDocGenerator;
use Dreamcat\ApiDoc\Error\GeneratorError;
use Dreamcat\ApiDoc\Popo\RouterEntranceDefinition;
use Dreamcat\ApiDocTest\Demo\Ctl\ErrorController;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;

/**
 * 异常测试
 * @author vijay
 */
class ApiGenErrorTest extends TestCase
{
    /**
     * 测试复杂头
     * @return void
     */
    public function testHeaderComplex()
    {
        $this->expectExceptionMessage("Header 只能使用标量，不支持 object");
        $this->expectException(GeneratorError::class);
        $this->expectExceptionCode(GeneratorError::VAR_MUST_SCALA);

        $routeDef = new RouterEntranceDefinition();
        $routeDef->setMethod("POST")
            ->setAction(new ReflectionMethod(ErrorController::class, "withCompHeader"))
            ->setUri("/withCompHeader");
        $generator = new ApiDocGenerator();
        $generator->parseApi($routeDef);
    }

    public function testPathVariableMul()
    {
        $this->expectExceptionMessage("Cookie 不支持多级路径 aa/bb");
        $this->expectException(GeneratorError::class);
        $this->expectExceptionCode(GeneratorError::VAR_PATH_CANT_MUL);

        $routeDef = new RouterEntranceDefinition();
        $routeDef->setMethod("POST")
            ->setAction(new ReflectionMethod(ErrorController::class, "withMulPath"))
            ->setUri("/withNoPath");
        $generator = new ApiDocGenerator();
        $generator->parseApi($routeDef);
    }

    public function testPathVariableUndefine()
    {
        $this->expectExceptionMessage("未定义的 PathVariable 变量 p");
        $this->expectException(GeneratorError::class);
        $this->expectExceptionCode(GeneratorError::PATH_VAR_UNDEFINED);

        $routeDef = new RouterEntranceDefinition();
        $routeDef->setMethod("POST")
            ->setAction(new ReflectionMethod(ErrorController::class, "withPath"))
            ->setUri("/withNoPath");
        $generator = new ApiDocGenerator();
        $generator->parseApi($routeDef);
    }
}

# end of file
